package com.agile.leetcode.http.util;

import com.agile.leetcode.http.protocol.Request;
import com.agile.leetcode.http.protocol.Response;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/5/24 21:44
 */
public final class ProtocolUtil {
    public static Request readRequest(InputStream in) throws IOException {
        // 读取编码
        byte[] byteEncode = new byte[1];
        in.read(byteEncode);
        // 拿到编码
        byte encode = byteEncode[0];
        // 读取命令长度
        byte[] commandLengthByte = new byte[4];
        in.read(commandLengthByte);
        int i = ByteUtil.byte2Int(commandLengthByte);

        // d读取命令
        byte[] commandBytes = new byte[i];
        in.read(commandBytes);
        String command = "";
        if (encode == Encode.UTF8) {
            command = new String(commandBytes, "utf-8");
        }
        if (encode == Encode.GBK) {
            command = new String(commandBytes, "GBK");
        }
        Request request = new Request(encode, command, i);
        return request;
    }

    public static Response readResponse(InputStream in) throws IOException {
        // 读取编码
        byte[] byteEncode = new byte[1];
        in.read(byteEncode);
        // 拿到编码
        byte encode = byteEncode[0];
        // 读取命令长度
        byte[] commandLengthByte = new byte[4];
        in.read(commandLengthByte);
        int i = ByteUtil.byte2Int(commandLengthByte);

        // d读取命令
        byte[] commandBytes = new byte[i];
        in.read(commandBytes);
        String command = "";
        if (encode == Encode.UTF8) {
            command = new String(commandBytes, "utf-8");
        }
        if (encode == Encode.GBK) {
            command = new String(commandBytes, "GBK");
        }
        Response response = new Response(encode, command, i);
        return response;
    }

    /**
     * 序列化请求信息
     *
     * @param out
     * @param request
     */
    public static void writeRequest(OutputStream out, Request request) throws IOException {
        // 拿到编码
        out.write(request.getCode());
        // output.write(response.getResponseLength());直接write一个int类型会截取低8位传输丢弃高24位
        out.write(ByteUtil.int2ByteArray(request.getCommandLength()));
        if (Encode.UTF8 == request.getCode()) {
            out.write(request.getCommand().getBytes("utf-8"));
         //   out.write(ByteUtil.int2ByteArray(request.getCommand().getBytes("utf-8").length));
        }
        if (request.getCode() == Encode.GBK) {
            out.write(request.getCommand().getBytes("GBK"));
         //   out.write(ByteUtil.int2ByteArray(request.getCommand().getBytes("GBK").length));
        }
        // 刷新,会发送到服务端
        out.flush();
    }

    /**
     * 序列化响应信息
     *
     * @param out
     * @param response
     * @throws IOException
     */
    public static void writeResponse(OutputStream out, Response response) throws IOException {
        // 拿到编码
        out.write(response.getEncode());
        //
        out.write(ByteUtil.int2ByteArray(response.getCommandLength()));
        //
        if (response.getEncode() == Encode.UTF8) {
            out.write(response.getContent().getBytes("utf-8"));
        }
        if (response.getEncode() == Encode.GBK) {
            out.write(response.getContent().getBytes("gbk"));
        }
        // 会发送到客户端
        out.flush();
    }
}
